package Sort.sort2022;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2022-04-11
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] array = {3, 5, 8, 4, 1, 6, 2, 9, 7};
        insertSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }

    public static void insertSort(int[] array) {
        if (array == null) {
            return;
        }

        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int value = array[i];
            while (j >= 0 && array[j] > value) {
                array[j + 1] = array[j];
                j--;
            }
            array[j + 1] = value;
        }
    }
}
